package org.mfoyou.agent.center.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mfoyou.agent.center.dao.inf.HuawuAgentInfoMapper;
import org.mfoyou.agent.center.dao.inf.HuawuAgentStoreSettingMapper;
import org.mfoyou.agent.center.dao.inf.HuawuIdcardInfoMapper;
import org.mfoyou.agent.center.dao.inf.HuawuServiceComponentMapper;
import org.mfoyou.agent.center.dao.inf.HuawuServiceNoticeMapper;
import org.mfoyou.agent.center.dao.inf.HuawuServicePriceMapper;
import org.mfoyou.agent.center.dao.inf.HuawuUserMapper;
import org.mfoyou.agent.center.dao.inf.HuawuWorkerMapper;
import org.mfoyou.agent.center.dao.inf.HuawuYouzanMapper;
import org.mfoyou.agent.center.dao.inf.MfoyouRegionCustomMapper;
import org.mfoyou.agent.center.dao.inf.MfoyouStationMasterMapper;
import org.mfoyou.agent.center.dao.mfoyou.inf.MfoyouSystemMapper;
import org.mfoyou.agent.common.dao.domain.HuawuAgentInfo;
import org.mfoyou.agent.common.dao.domain.HuawuAgentStoreSetting;
import org.mfoyou.agent.common.dao.domain.HuawuServiceComponent;
import org.mfoyou.agent.common.dao.domain.HuawuServiceComponentExample;
import org.mfoyou.agent.common.dao.domain.HuawuServiceNotice;
import org.mfoyou.agent.common.dao.domain.HuawuServicePrice;
import org.mfoyou.agent.common.dao.domain.HuawuUser;
import org.mfoyou.agent.common.dao.domain.HuawuUserExample;
import org.mfoyou.agent.common.dao.domain.HuawuWorker;
import org.mfoyou.agent.common.dao.domain.HuawuWorkerExample;
import org.mfoyou.agent.common.dao.domain.HuawuWorkerExample.Criteria;
import org.mfoyou.agent.common.dao.domain.HuawuYouzan;
import org.mfoyou.agent.common.dao.domain.HuawuYouzanExample;
import org.mfoyou.agent.common.dao.domain.MfoyouRegionCustom;
import org.mfoyou.agent.common.dao.domain.MfoyouRegionCustomExample;
import org.mfoyou.agent.common.dao.domain.MfoyouStationMaster;
import org.mfoyou.agent.common.dao.domain.MfoyouStationMasterExample;
import org.mfoyou.agent.common.entity.HuawuAgentWokerInfo;
import org.mfoyou.agent.common.entity.HuawuAgentWorkerData;
import org.mfoyou.agent.common.entity.StationMasterInAgent;
import org.mfoyou.agent.common.inf.IIdentity;
import org.mfoyou.agent.utils.common.ArrayListObject;
import org.mfoyou.agent.utils.sql.MybatisGenerator;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IdentityService implements IIdentity {

    org.slf4j.Logger logger = LoggerFactory.getLogger(IdentityService.class);

    @Autowired
    HuawuServiceComponentMapper huawuServiceComponentMapper;
    @Autowired
    HuawuWorkerMapper huawuWorkerMapper;
    @Autowired
    HuawuAgentInfoMapper huawuAgentInfoMapper;
    @Autowired
    HuawuAgentStoreSettingMapper huawuAgentStoreSettingMapper;
    @Autowired
    HuawuServicePriceMapper huawuServicePriceMapper;
    @Autowired
    HuawuIdcardInfoMapper huawuIdcardInfoMapper;
    @Autowired
    MfoyouStationMasterMapper mfoyouStationMasterMapper;
    @Autowired
    MfoyouSystemMapper mfoyouSystemMapper;
    @Autowired
    HuawuUserMapper huawuUserMapper;
    @Autowired
    HuawuServiceNoticeMapper huawuServiceNoticeMapper;
    @Autowired
    MfoyouRegionCustomMapper mfoyouRegionCustomMapper;
    @Autowired
    HuawuYouzanMapper huawuYouzanMapper;

    @Override
    public HuawuWorker getMfoyouWorkerInfo(Integer userId, Integer agentId) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        if (agentId != null && agentId != 0) {
            huawuWorkerExample.or().andUserIdEqualTo(userId).andAgentIdEqualTo(agentId);
        } else {
            huawuWorkerExample.or().andUserIdEqualTo(userId);
        }
        List<HuawuWorker> selectByExample = huawuWorkerMapper.selectByExample(huawuWorkerExample);
        return MybatisGenerator.getOneValue(selectByExample);
    }

    @Override
    public HuawuWorker getMfoyouWorkerInfoV2(Integer userId, Integer agentId) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        huawuWorkerExample.or().andUserIdEqualTo(userId);
        List<HuawuWorker> selectByExample = huawuWorkerMapper.selectByExample(huawuWorkerExample);
        return MybatisGenerator.getOneValue(selectByExample);
    }

    @Override
    public List<HuawuWorker> getMfoyouWorkerList(List<Integer> arrUser, Integer stationId) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        Criteria andUserIdIn = huawuWorkerExample.or().andUserIdIn(arrUser);
        if (stationId != null) {
            andUserIdIn.andStationIdEqualTo(stationId);
        }
        return huawuWorkerMapper.selectByExample(huawuWorkerExample);
    }

    @Override
    public int modifyMfoyouWorkerInfo(HuawuWorker huawuWorker) {
        if (huawuWorker.getUserId() == null) {
            return 0;
        }
        return huawuWorkerMapper.updateByPrimaryKeySelective(huawuWorker);
    }

    @Override
    public HuawuAgentInfo getAgentInfo(Integer userId) {
        return huawuAgentInfoMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int modifyMfoyouAgent(HuawuAgentInfo mfoyouAgent) {
        return huawuAgentInfoMapper.updateByPrimaryKeySelective(mfoyouAgent);
    }

    @Override
    public ArrayListObject<HuawuWorker> getMfoyouWorkerList(HuawuWorker mfoyouWorker, Integer pageIndex, Integer pageSize, Date startDate, Date endDate) {
        HuawuWorkerExample example = new HuawuWorkerExample();
        MybatisGenerator.setLimit(example, pageIndex, pageSize);
        Criteria criteria = example.or();
        if (startDate != null) {
            criteria.andCreateTimeGreaterThan(startDate);
        }
        if (endDate != null) {
            criteria.andCreateTimeLessThan(endDate);
        }
        MybatisGenerator.makeExample(mfoyouWorker, criteria);
        example.setOrderByClause("worker_join_time desc");
        int count = huawuWorkerMapper.countByExample(example);
        List<HuawuWorker> list = huawuWorkerMapper.selectByExample(example);
        return ArrayListObject.bliudArrayList(list, pageIndex, pageSize, count);
    }

    @Override
    public HuawuAgentStoreSetting getSpecialSettingV1(Integer agentId) {
        return huawuAgentStoreSettingMapper.selectByPrimaryKey(agentId);
    }

    @Override
    public HuawuServicePrice getServicePrice(Integer agentId) {
        HuawuServicePrice selectByPrimaryKey = huawuServicePriceMapper.selectByPrimaryKey(agentId);
        if (selectByPrimaryKey == null) {
            selectByPrimaryKey = huawuServicePriceMapper.selectByPrimaryKey(0);
            selectByPrimaryKey.setCreateTime(null);
            selectByPrimaryKey.setAgentId(agentId);
            selectByPrimaryKey.setModifyTime(null);
            huawuServicePriceMapper.insertSelective(selectByPrimaryKey);
        }
        return selectByPrimaryKey;
    }

    @Override
    public ArrayListObject<?> getMfoyouWorkerListV2(HuawuWorker mfoyouWorker, Integer pageid, Integer pageSize,
                                                    Date startDate, Date endDate) {
        HuawuWorkerExample mfoyouWorkerExample = new HuawuWorkerExample();
        Criteria or = mfoyouWorkerExample.or();
        MybatisGenerator.makeExample(mfoyouWorker, or);
        int count = huawuWorkerMapper.countByExample(mfoyouWorkerExample);
        pageid = pageid == null ? 1 : pageid;
        pageSize = pageSize == null ? 10 : pageSize;
        Integer pageStart = (pageid - 1) * pageSize;
        List<HuawuAgentWokerInfo> list = huawuWorkerMapper.getWokerInfoByagentInfo(mfoyouWorker, startDate, endDate, pageStart, pageSize);
        for (HuawuAgentWokerInfo mfoyouAgentWokerInfo : list) {
            mfoyouAgentWokerInfo.setMfoyouIdcardInfo(huawuIdcardInfoMapper.selectByPrimaryKey(mfoyouAgentWokerInfo.getUserId()));
            if (mfoyouAgentWokerInfo.getStationId() != null) {
                mfoyouAgentWokerInfo.setStationName(mfoyouStationMasterMapper.selectByPrimaryKey(mfoyouAgentWokerInfo.getStationId()).getStationName());
            }
        }
        return ArrayListObject.bliudArrayList(list, pageid, pageSize, count);
    }

    @Override
    public ArrayListObject<HuawuAgentWorkerData> getMfoyouWorkerListV3(HuawuWorker mfoyouWorker, Integer pageId, Integer pageSize) {
        HuawuWorkerExample mfoyouWorkerExample = new HuawuWorkerExample();
        Criteria or = mfoyouWorkerExample.or();
//		 ArrayList<Integer> integers = new ArrayList<>();
//		 integers.add(-1);integers.add(5);
//		 or.andWorkerStateNotIn(integers);
        or.andWorkerStateNotEqualTo(-1);
        MybatisGenerator.makeExample(mfoyouWorker, or);
        int count = huawuWorkerMapper.countByExample(mfoyouWorkerExample);
        if (mfoyouWorker.getStationId() != null) {
//	        	or.andStationIdEqualTo(mfoyouWorker.getStationId());
            mfoyouWorkerExample.setUserId(mfoyouWorker.getStationId());
            mfoyouWorkerExample.setUserType(0);
        } else if (mfoyouWorker.getAgentId() != 0) {
            mfoyouWorkerExample.setUserId(mfoyouWorker.getAgentId());
            mfoyouWorkerExample.setUserType(1);
        }
        List<HuawuAgentWorkerData> mfoyouAgentWorkerDatas = huawuWorkerMapper.selectAgentByExample(mfoyouWorkerExample);
        return ArrayListObject.bliudArrayList(mfoyouAgentWorkerDatas, pageId, pageSize, count);
    }

    /**
     * 更新代理商计费规则
     *
     * @param mfoyouServicePricev2
     * @return
     */
    @Override
    public int updatePriceV2ByagentId(HuawuServicePrice mfoyouServicePricev2) {
        HuawuServicePrice selectByPrimaryKey = huawuServicePriceMapper.selectByPrimaryKey(mfoyouServicePricev2.getAgentId());
        if (selectByPrimaryKey == null) {
            return huawuServicePriceMapper.insertSelective(mfoyouServicePricev2);
        }
        return huawuServicePriceMapper.updateByPrimaryKeySelective(mfoyouServicePricev2);
    }

    @Override
    public int addWorker(HuawuWorker workerInfo) {
        return huawuWorkerMapper.insertSelective(workerInfo);
    }

    @Override
    public int updateMfoyouAgentSetting(HuawuAgentStoreSetting mfoyouAgentStoreSetting) {
        HuawuAgentStoreSetting selectByPrimaryKey = huawuAgentStoreSettingMapper.selectByPrimaryKey(mfoyouAgentStoreSetting.getAgentId());
        if (selectByPrimaryKey == null) {
            return huawuAgentStoreSettingMapper.insertSelective(mfoyouAgentStoreSetting);
        }
        return huawuAgentStoreSettingMapper.updateByPrimaryKeySelective(mfoyouAgentStoreSetting);
    }

    @Override
    public HuawuWorker getWorkerByPhone(String phone) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        huawuWorkerExample.or().andWorkerPhoneEqualTo(phone);
        List<HuawuWorker> selectByExample = huawuWorkerMapper.selectByExample(huawuWorkerExample);
        return MybatisGenerator.getOneValue(selectByExample);
    }

    @Override
    public List<HuawuWorker> getMfoyouWorkerListByAgentId(Integer agentId, Integer agentStationId) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        Criteria andWorkerStateNotEqualTo = huawuWorkerExample.or().andWorkerOpenStateEqualTo(1)
                .andAgentIdEqualTo(agentId).andWorkerStateNotEqualTo(-1);
        if (agentStationId != null) {
            andWorkerStateNotEqualTo.andStationIdEqualTo(agentStationId);
        }
        return huawuWorkerMapper.selectByExample(huawuWorkerExample);
    }

    @Override
    public int addStation(MfoyouStationMaster mfoyouStationMaster) {
        return mfoyouStationMasterMapper.insertSelective(mfoyouStationMaster);
    }

    @Override
    public MfoyouStationMaster getStationMasterByUserId(Integer userId) {
        return mfoyouStationMasterMapper.selectByPrimaryKey(userId);
    }

    @Override
    public MfoyouStationMaster getMfoyouStationInfo(Integer stationId) {
        if (stationId == null) {
            return null;
        }
        return mfoyouStationMasterMapper.selectByPrimaryKey(stationId);
    }

    @Override
    public ArrayListObject<?> getStationMaster(MfoyouStationMaster mfoyouStationMaster, Date sDate, Date eDate,
                                               Integer pageSize, Integer pageId) {
        Integer pagestart = null;
        if (pageSize != null && pageId != null) {
            pagestart = pageSize * (pageId - 1);
        }
        List<StationMasterInAgent> selectByAgent = mfoyouStationMasterMapper.selectByAgent(mfoyouStationMaster.getAgentId(), mfoyouStationMaster.getPowerState(),
                sDate, eDate, pagestart, pageSize);
        int count = mfoyouSystemMapper.selectCount();
        return ArrayListObject.bliudArrayList(selectByAgent, pageId, pageSize, count);
    }

    @Override
    public int updateByAgent(MfoyouStationMaster mfoyouStationMaster) {
        return mfoyouStationMasterMapper.updateByPrimaryKeySelective(mfoyouStationMaster);
    }

    @Override
    public int deleteByPrimartKey(MfoyouStationMaster mfoyouStationMaster) {
        MfoyouStationMaster mfoyouStationMaster2 = mfoyouStationMasterMapper.selectByPrimaryKey(mfoyouStationMaster.getStationId());
        if (mfoyouStationMaster2 == null) {
            return -2;//station不存在
        }
        if (!mfoyouStationMaster2.getAgentId().equals(mfoyouStationMaster.getAgentId())) {
            return -3;
        }
        int deleteByPrimaryKey = mfoyouStationMasterMapper.deleteByPrimaryKey(mfoyouStationMaster.getStationId());
        //清空商店这边站长数据
        String sql = "update huawu_store_info set store_station_id = null where store_station_id = " + mfoyouStationMaster.getStationId();
        //清空跑腿员数据
        String sql2 = "update huawu_worker set station_id = null where station_id = " + mfoyouStationMaster.getStationId();
        int update_item = mfoyouSystemMapper.update_item(sql);
        int update_item2 = mfoyouSystemMapper.update_item(sql2);
        HuawuUserExample huawuUserExample = new HuawuUserExample();
        huawuUserExample.or().andAgentIdEqualTo(-3).andUserIdEqualTo(mfoyouStationMaster.getStationId());
        int deleteByExample = huawuUserMapper.deleteByExample(huawuUserExample);
        logger.info("更新结果 " + update_item + "::" + update_item2 + "::" + deleteByExample);
        MfoyouRegionCustomExample mfoyouRegionCustomExample = new MfoyouRegionCustomExample();
        mfoyouRegionCustomExample.or().andStationIdEqualTo(mfoyouStationMaster.getStationId());
        mfoyouRegionCustomMapper.deleteByExample(mfoyouRegionCustomExample);
        return deleteByPrimaryKey;
    }

    @Override
    public List<MfoyouStationMaster> getStationByAgentId(Integer agentId) {
        MfoyouStationMasterExample mfoyouStationMasterExample = new MfoyouStationMasterExample();
        mfoyouStationMasterExample.or().andAgentIdEqualTo(agentId);
        return mfoyouStationMasterMapper.selectByExample(mfoyouStationMasterExample);
    }

    @Override
    public HuawuServiceNotice getServiceNotice(Integer agentId) {
        HuawuServiceNotice selectByPrimaryKey = huawuServiceNoticeMapper.selectByPrimaryKey(agentId);
        if (selectByPrimaryKey == null) {
            selectByPrimaryKey = new HuawuServiceNotice();
            selectByPrimaryKey.setNoticeState(0);
            selectByPrimaryKey.setAgentId(agentId);
            huawuServiceNoticeMapper.insertSelective(selectByPrimaryKey);
        }
        return selectByPrimaryKey;
    }

    @Override
    public int modifyServiceNotice(HuawuServiceNotice json2Object) {
        return huawuServiceNoticeMapper.updateByPrimaryKeySelective(json2Object);
    }

    @Override
    public List<MfoyouStationMaster> getStationByStationIds(List<Integer> stationIds) {
        MfoyouStationMasterExample mfoyouStationMasterExample = new MfoyouStationMasterExample();
        mfoyouStationMasterExample.or().andStationIdIn(stationIds);
        List<MfoyouStationMaster> selectByExample = mfoyouStationMasterMapper.selectByExample(mfoyouStationMasterExample);
        for (MfoyouStationMaster mfoyouStationMaster : selectByExample) {
            String sql = "select ifnull(count(*),0) runnerC from huawu_worker where station_id = " + mfoyouStationMaster.getStationId();
            Integer count = Integer.parseInt(mfoyouSystemMapper.select_item(sql).get("runnerC").toString());
            mfoyouStationMaster.setRunnerCount(count);
        }
        return selectByExample;
    }

    @Override
    public int modifyMfoyouRegionCustomByStationId(MfoyouRegionCustom mfoyouRegionCustom) {
        MfoyouRegionCustomExample mfoyouRegionCustomExample = new MfoyouRegionCustomExample();
        mfoyouRegionCustomExample.or().andStationIdEqualTo(mfoyouRegionCustom.getStationId());
        return mfoyouRegionCustomMapper.updateByExampleSelective(mfoyouRegionCustom, mfoyouRegionCustomExample);
    }

    @Override
    public int addServiceComponent(HuawuServiceComponent json2Object) {
        return huawuServiceComponentMapper.insertSelective(json2Object);
    }

    @Override
    public int updateServiceComponent(HuawuServiceComponent json2Object) {
        return huawuServiceComponentMapper.updateByPrimaryKeySelective(json2Object);
    }

    @Override
    public int deleteServiceComponent(Integer componentId, Integer agentId) {
        HuawuServiceComponentExample huawuServiceComponentExample = new HuawuServiceComponentExample();
        huawuServiceComponentExample.or().andComponentIdEqualTo(componentId).andAgentIdEqualTo(agentId);
        return huawuServiceComponentMapper.deleteByExample(huawuServiceComponentExample);
    }

    @Override
    public List<HuawuServiceComponent> getServiceComponentByAgentId(Integer agentId, Integer state, Integer componentId) {
        HuawuServiceComponentExample huawuServiceComponentExample = new HuawuServiceComponentExample();
        org.mfoyou.agent.common.dao.domain.HuawuServiceComponentExample.Criteria andAgentIdEqualTo = huawuServiceComponentExample.or().andAgentIdEqualTo(agentId);
        if (state != null) {
            andAgentIdEqualTo.andComponentStateEqualTo(state);
        }
        if (componentId != null) {
            andAgentIdEqualTo.andComponentIdEqualTo(componentId);
        }
        HuawuServicePrice huawuServicePrice = huawuServicePriceMapper.selectByPrimaryKey(agentId);
        List<HuawuServiceComponent> huawuServiceComponents = huawuServiceComponentMapper.selectByExample(huawuServiceComponentExample);
        if (huawuServiceComponents.size() > 0) {
            HuawuServiceComponent huawuServiceComponent = huawuServiceComponents.get(0);
            huawuServiceComponent.setServiceDo(huawuServicePrice.getServiceDo());
        }
        return huawuServiceComponents;
    }

    @Override
    public Map<String, Object> getSortKey(Integer agentId, String phoneNumber) {
        String sql = "select store_sort_key,store_id,store_name from huawu_store_info where store_agent_id =  " + agentId
                + " and store_sort_key is not null  order by store_sort_key desc";
        List<Map<String, Object>> select_list = mfoyouSystemMapper.select_list(sql);
        Map<String, Object> map = new HashMap<>();
        map.put("data", select_list);
        if (phoneNumber != null) {
            HuawuUserExample huawuUserExample = new HuawuUserExample();
            huawuUserExample.or().andUserPhoneEqualTo(phoneNumber).andAgentIdEqualTo(-2);
            List<HuawuUser> selectByExample = huawuUserMapper.selectByExample(huawuUserExample);
            if (selectByExample.size() == 1) {
                HuawuUser huawuUser = selectByExample.get(0);
                String sql2 = "select store_id,store_sort_key,store_name from huawu_store_info where store_id =" + huawuUser.getUserId();
                Map<String, Object> select_item = mfoyouSystemMapper.select_item(sql2);
                map.put("searchData", select_item);
            }
        }
        return map;
    }

    @Override
    public int unsetSortKey(Integer agentId, Integer storeId) {
        String sql = "update huawu_store_info set store_sort_key = null where store_id = " + storeId
                + " and store_agent_id =  " + agentId;
        return mfoyouSystemMapper.update_item(sql);
    }

    @Override
    public int setSortKey(Integer agentId, Integer storeId, Integer key) {
        String sql = "select ifnull(count(1),0) count from huawu_store_info where store_agent_id = " + agentId + " and  store_sort_key = " + key;
        boolean isIn = Integer.parseInt(mfoyouSystemMapper.select_item(sql).get("count").toString()) > 0;
        if (isIn) return -2;
        String sql2 = "update huawu_store_info set store_sort_key = " + key + " where store_id = " + storeId
                + " and store_agent_id =   " + agentId;
        return mfoyouSystemMapper.update_item(sql2);
    }

    @Override
    public int addYouzanData(HuawuYouzan huawuYouzan) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        huawuYouzanExample.or().andYouzanStoreIdEqualTo(huawuYouzan.getYouzanStoreId());
        huawuYouzanExample.or().andUserIdEqualTo(huawuYouzan.getUserId());
        List<HuawuYouzan> selectByExample = huawuYouzanMapper.selectByExample(huawuYouzanExample);
        if (selectByExample.size() > 0) {
            return huawuYouzanMapper.updateByExampleSelective(huawuYouzan, huawuYouzanExample);
        } else {
            return huawuYouzanMapper.insertSelective(huawuYouzan);
        }
    }

    @Override
    public HuawuYouzan getUserIdByYouzanStoreId(String youzan_storeid) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        huawuYouzanExample.or().andYouzanStoreIdEqualTo(youzan_storeid);
        return MybatisGenerator.getOneValue(huawuYouzanMapper.selectByExample(huawuYouzanExample));
    }

    @Override
    public int addYouzanStore(HuawuYouzan huawuYouzan) {
        return huawuYouzanMapper.insertSelective(huawuYouzan);
    }

    @Override
    public ArrayListObject<?> getYouzanStores(Integer pageId, Integer pagesize, Integer userId, Integer agentId) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        HuawuYouzanExample.Criteria or = huawuYouzanExample.or();
        if (userId != null) {
            or.andUserIdEqualTo(userId);
        }
        or.andAgentIdEqualTo(agentId);
        int count = huawuYouzanMapper.countByExample(huawuYouzanExample);
        MybatisGenerator.setLimit(huawuYouzanExample, pageId, pagesize);
        List<HuawuYouzan> selectByExample = huawuYouzanMapper.selectByExample(huawuYouzanExample);
        return ArrayListObject.bliudArrayList(selectByExample, pageId, pagesize, count);
    }

    @Override
    public int updateYouzan(HuawuYouzan huawuYouzan) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        org.mfoyou.agent.common.dao.domain.HuawuYouzanExample.Criteria andUserIdEqualTo = huawuYouzanExample.or().andUserIdEqualTo(huawuYouzan.getUserId());
        if (huawuYouzan.getAgentId() != null) {
            andUserIdEqualTo.andAgentIdEqualTo(huawuYouzan.getAgentId());
        }
        huawuYouzan.setUserId(null);
        return huawuYouzanMapper.updateByExampleSelective(huawuYouzan, huawuYouzanExample);
    }

    @Override
    public int deleteYouzan(HuawuYouzan huawuYouzan) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        huawuYouzanExample.or().andUserIdEqualTo(huawuYouzan.getUserId()).andAgentIdEqualTo(huawuYouzan.getAgentId());
        return huawuYouzanMapper.deleteByExample(huawuYouzanExample);
    }

    @Override
    public HuawuYouzan getYouzanByUserId(Integer userId) {
        HuawuYouzanExample huawuYouzanExample = new HuawuYouzanExample();
        if (userId != null) {
            huawuYouzanExample.or().andUserIdEqualTo(userId);
            return MybatisGenerator.getOneValue(huawuYouzanMapper.selectByExample(huawuYouzanExample));
        }
        return null;
    }

    @Override
    public List<Integer> getWorkerInStation(Integer stationId) {
        String sql = "select user_id  from huawu_worker where worker_state > -1 and station_id = " + stationId;
        List<Map<String, Object>> select_list = mfoyouSystemMapper.select_list(sql);
        if (select_list == null) return null;
        List<Integer> integers = new ArrayList<>();
        for (Map<String, Object> map : select_list) {
            Integer parseInt = Integer.valueOf(map.get("user_id").toString());
            integers.add(parseInt);
        }
        return integers;
    }

    @Override
    public List<HuawuWorker> getMfoyouWorkerListByAgentIdExcept(Integer agentId, Integer userId, Integer stationId) {
        HuawuWorkerExample huawuWorkerExample = new HuawuWorkerExample();
        Criteria andUserIdNotEqualTo = huawuWorkerExample.or().andWorkerOpenStateEqualTo(1).andAgentIdEqualTo(agentId)
                .andWorkerStateNotEqualTo(-1).andUserIdNotEqualTo(userId);
        if (stationId != null) {
            andUserIdNotEqualTo.andStationIdEqualTo(stationId);
        }
        return huawuWorkerMapper.selectByExample(huawuWorkerExample);
    }

    @Override
    public int updateMfoyouAgentShopSetting(HuawuAgentStoreSetting mfoyouAgentStoreSetting) {
        return huawuAgentStoreSettingMapper.updateByPrimaryKeySelective(mfoyouAgentStoreSetting);
    }

    @Override
    public HuawuAgentStoreSetting getMfoyouAgentShopSettingByAgentId(int agentId) {
        return huawuAgentStoreSettingMapper.selectByPrimaryKey(agentId);
    }

    @Override
    public Integer getStoreIdByNameKey(String key, Integer agentId) {
        String sql = "select store_id from huawu_store_info where store_name like '%" + key + "%' and store_agent_id = " + agentId;
        Map<String, Object> select_item = mfoyouSystemMapper.select_item(sql);
        if (select_item == null) {
            return null;
        }
        if (select_item.get("store_id") == null) {
            return null;
        }
        return Integer.parseInt(select_item.get("store_id").toString());//需要改
    }
}
